package znbkTopicType.resResponse;

import android.content.Context;
import android.os.Handler;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import lancooControl.CustomAnswerChoiceView;

import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTopicType.resContent.ZnbkContentResStlPptAdapter.TotalItemCount;
import static znbkTopicType.resContent.ZnbkContentResStlPptAdapter.analysis;
import static znbkTopicType.resContent.ZnbkContentResStlPptAdapter.optionContent;
import static znbkTopicType.resContent.ZnbkContentResStlPptAdapter.optionIndex;
import static znbkTopicType.resContent.ZnbkContentResStlPptAdapter.smallSubChileAnswer;
import static znbkTopicType.resContent.ZnbkContentResStlPptAdapter.sumOption;

/**
 * Created by Administrator on 2018\3\19 0019.
 */

public class ZnbkResponseResStlPptAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试题的ID
    private int PaperID;
    //当前小题ID
    private int OptionID;
    private Context mContext;

    private int ModelFrom;


    private int tvBlack;
    private int tvBlue;
    private int tvWhite;
    private int tvGreen;
    private int tvRed;
    private int btnWhite;
    private int btnBlue;
    private int btnGreen;
    private int btnRed;


    private ViewHolder holder = null;
    private class ViewHolder{
        TextView TvItemContent;//大题文本题干
        LinearLayout LayoutOptions;//选项显示区
        LinearLayout LayoutCheckAnalysis;//答案显示区
        LinearLayout LayoutStuAnswer;
        LinearLayout LayoutAnswer;
        LinearLayout LayoutAnalysis;
        TextView TvAnswer;//正确答案
        TextView TvAnalysis;//解析
        TextView TvAnswerAmount;//小题总数

        private ViewHolder(View convertView) {
            TvItemContent = (TextView) convertView.findViewById(R.id.tv_item_content);
            LayoutOptions = (LinearLayout) convertView.findViewById(R.id.layout_options);
            LayoutCheckAnalysis = (LinearLayout) convertView.findViewById(R.id.layout_checkAnalysis);
            LayoutStuAnswer = (LinearLayout) convertView.findViewById(R.id.layout_stuAnswer);
            LayoutAnswer = (LinearLayout) convertView.findViewById(R.id.layout_answer);
            LayoutAnalysis = (LinearLayout) convertView.findViewById(R.id.layout_analysis);
            TvAnswer = (TextView) convertView.findViewById(R.id.tv_answer);
            TvAnalysis = (TextView) convertView.findViewById(R.id.tv_analysis);
            TvAnswerAmount = (TextView) convertView.findViewById(R.id.tv_answerAmount);

        }
    }

    private CustomAnswerChoiceView[] ChoiceAnswer;//选项信息


    public ZnbkResponseResStlPptAdapter(Context context, int sumItem, int paperID, int optionID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.OptionID = optionID;
        this.ModelFrom = modelFrom;

        tvBlack = mContext.getResources().getColor(R.color.black);
        tvBlue = mContext.getResources().getColor(R.color.blue);
        tvWhite = mContext.getResources().getColor(R.color.white);
        tvGreen = mContext.getResources().getColor(R.color.green);
        tvRed = mContext.getResources().getColor(R.color.red);

        btnWhite = R.mipmap.option_normal;
        btnBlue = R.mipmap.option_select;
        btnGreen = R.mipmap.option_right;
        btnRed = R.mipmap.option_wrong;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_response_res_xz_item, null);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        * 获取试题相关信息
        * */
        getQuesInfor();

        /*
        * 展示作答区内容及实现作答流程
        * */
        showQuesInfor();

        /*
        * 已提交状态下，初始化答案解析
        * */
        showAnalysis();

        return convertView;
    }


    /*
* 获取小题选项信息
* */
    private String typeName;//试题类型


    /*
    * 获取试题相关信息
    * */
    private void getQuesInfor(){

        //试题类型
        typeName = mEntityRdtz.getResFromTK().getTypeName();
    }

    /*
    * 展示试题信息
    * */
    Handler htmlHandlerItemContent = new Handler();
    private void showQuesInfor(){

        holder.TvAnswerAmount.setText("本题包含"+TotalItemCount+"个小题");

        /*
        * 显示题目题干
        * */
        String itemContent = StringEscapeUtils.unescapeXml("(" + String.valueOf(OptionID+1) + ")");

        final String finalItemContent = itemContent;
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                final Spanned tvItemContent = Html.fromHtml(finalItemContent, Control.imgGetter,null);
                htmlHandlerItemContent.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        holder.TvItemContent.setText(tvItemContent);
                        holder.TvItemContent.setTextSize(18);
                    }
                });
            }
        }).start();

        /*
        * 显示选项内容
        * */
        ChoiceAnswer = new CustomAnswerChoiceView[sumOption];
        for(int i =0;i<sumOption;i++){
            ChoiceAnswer[i] = new CustomAnswerChoiceView(mContext);
            ChoiceAnswer[i].setAnswerChoice(optionIndex[i],tvBlack,btnWhite,optionContent[i],tvBlack);

            final int finalI = i;
            ChoiceAnswer[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    return;
                }
            });

            ChoiceAnswer[i].BtnChoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    return;
                }
            });

            holder.LayoutOptions.addView(ChoiceAnswer[i]);

        }

        int currentTrueOption = Integer.valueOf(Control.stringToAscii(smallSubChileAnswer[OptionID])) - 65;
        for (int k = 0; k < sumOption; k++) {
            if(currentTrueOption == k){
                ChoiceAnswer[k].setAnswerChoice(optionIndex[k],tvWhite,btnGreen,optionContent[k],tvGreen);
            }
        }


    }


    /*
    * 已提交状态试题分析部分内容展示
    * */
    private void showAnalysis() {


        //显示题目回看解析内容
        holder.LayoutCheckAnalysis.setVisibility(View.VISIBLE);
        holder.LayoutStuAnswer.setVisibility(View.GONE);

        //正确答案
        holder.TvAnswer.setText(smallSubChileAnswer[OptionID]);
        holder.TvAnswer.setTextColor(tvGreen);
        //答案解析
        if (!analysis.equals("")) {
            holder.TvAnalysis.setText(analysis);
        } else {
            holder.TvAnalysis.setText(R.string.znbk_no_analysis);
        }
        holder.TvAnalysis.setTextColor(tvBlack);

    }

}